package com.ygkj.demo

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Context.USB_SERVICE
import android.content.Intent
import android.hardware.usb.UsbManager
import android.util.Log
import android.widget.Toast

class USBReceiver: BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent) {
        // 这里可以拿到插入的USB设备对象
        when (intent.action) {
            // 插入USB设备

            Intent.ACTION_MEDIA_MOUNTED-> {
                val mUsbManager = context!!.getSystemService(USB_SERVICE) as UsbManager
                val mDeviceMap = mUsbManager.deviceList
                for (value in mDeviceMap.values) {
                    var deviceName = value.deviceName
                    Log.d("deviceName",deviceName)
                }

                Toast.makeText(context,"open",Toast.LENGTH_LONG).show()

            }
            // 拔出USB设备
            Intent.ACTION_MEDIA_UNMOUNTED -> {
                Toast.makeText(context,"close",Toast.LENGTH_LONG).show()
            }
           else->{
               Toast.makeText(context,"else",Toast.LENGTH_LONG).show()
           }
        }

    }
}